<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tolls
          :tree-data="company"
          :is-root="true"
          @addDepts="addDeptsFn"
        />
        <!-- 树形控件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tolls
            slot-scope="{ data }"
            :tree-data="data"
            @addDepts="addDeptsFn"
            @delTree="getDepartments"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDailog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import treeTolls from './components/tree-tolls.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: { treeTolls, AddDept },
  data() {
    return {
      loading: false, // 用来控制进度弹层的显示和隐藏
      company: {
        name: '',
        manager: '负责人',
        id: ''
      },
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDailog: false,
      node: null // 当前操作的节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取树形组织架构数据
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()
      this.company.name = result.companyName
      this.departs = tranListToTreeData(result.depts, '')
      this.loading = false
    },
    // 点击添加部门
    addDeptsFn(node) {
      this.node = node
      console.log(this.node)
      this.showDailog = true
    },
    // 点击编辑部门
    async editDepts(node) {
      this.node = node
      await this.$refs.addDept.getDepartDetail(node.id)
      this.showDailog = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
